package com.stars.home.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.core.net.toUri
import androidx.lifecycle.lifecycleScope
import com.baidu.mapapi.search.core.PoiInfo
import com.eazypermissions.common.model.PermissionResult
import com.eazypermissions.coroutinespermission.PermissionManager
import com.google.gson.Gson
import com.luck.picture.lib.PictureSelector
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.base.MBActivity
import com.stars.home.config.MIntentAction
import com.stars.home.databinding.ActivityCertificationBinding
import com.stars.home.inte.ChoiceData
import com.stars.home.inte.OnUploadCallback
import com.stars.home.model.bean.UpImgBean
import com.stars.home.utils.ChosePhotoUtils
import com.stars.home.utils.FileUtils
import com.stars.home.utils.OssUtils
import com.stars.home.utils.PopuListUtils
import com.stars.home.vmodel.CertificationVModel
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch
import java.util.ArrayList

/**
 * 认证信息
 *on 2022/9/25
 */
class CertificationActivity : MBActivity<CertificationVModel, ActivityCertificationBinding>(),
    OnUploadCallback<UpImgBean> {



    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
         vm.lessType=intent.extras?.getString("type","")

        vm.cityList.observe(this){
            if(it.isEmpty()){

                toast("暂时没有城市数据")
                return@observe
            }
       // 去选择
            var list=ArrayList<String>()

            for(item in it){
                list.add(item.name)
            }
            PopuListUtils.showQMUIPopup(context(),binding.llCity,list,object : ChoiceData {
                override fun onRestuse(context: String?) {
                    for(item in it){
                        if(item.name == context){
                            vm.cityId=item.id
                            binding.tvCity.text=item.name
                        }
                    }

                }

            })



        }


    }
    override fun listeners() {
        super.listeners()

        binding.llCity.onClick {
            //选择城市
            vm.getCityList()

        }
        binding.llPoint.onClick {

            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                        this@CertificationActivity, 1000,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) is PermissionResult.PermissionGranted
                ) {
                    UiSwitch.singleRes(this@CertificationActivity,
                        ChoseBaiDuActivity::class.java, MIntentAction.REQUEST_CODE_ONE)
                }

            }
        }

        binding.flOne.onClick {
            vm.type=1
            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                        this@CertificationActivity, 1000,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) is PermissionResult.PermissionGranted
                ) {

                    ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)
                }

            }
        }
        binding.flTwo.onClick {
            vm.type=2
            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                        this@CertificationActivity, 1000,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) is PermissionResult.PermissionGranted
                ) {

                    ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)
                }

            }
        }
        binding.flThree.onClick {
            vm.type=3
            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                        this@CertificationActivity, 1000,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) is PermissionResult.PermissionGranted
                ) {

                    ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)
                }

            }
        }
        binding.flFour.onClick {
            vm.type=4
            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                        this@CertificationActivity, 1000,
                        Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE
                    ) is PermissionResult.PermissionGranted
                ) {

                    ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)
                }

            }
        }
    }
    override fun vmClass(): Class<CertificationVModel> {
        return CertificationVModel::class.java
    }
    override fun binding(): ActivityCertificationBinding {
        return ActivityCertificationBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return "实名认证"
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }

        if (requestCode == MIntentAction.REQUEST_CODE_ONE) {
            val bundle = data!!.extras
            if (null != bundle) {
                var data=bundle.getString("bean")
                val item: PoiInfo = Gson().fromJson(data, PoiInfo::class.java)

                vm.latitude=item.getLocation().latitude.toString()
                vm.longitude=item.getLocation().longitude.toString()
                vm.address=item.address
                binding.tvPoint.text = item.address

            }
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_THREE) {//图片

            val selectList = PictureSelector.obtainMultipleResult(data)
            val list = ArrayList<String>()
            if (null != selectList) {
                if(selectList[0].path.contains("content://")){

                    val cr = this.contentResolver

                    list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))

                }else{
                    list.add(selectList[0].path)
                }

              //  vm.toUpImg(list[0])

                OssUtils(this, vm)
                    .setUpLoadMore("img", list, vm.type, this)

            }

            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            if(selectList[0].path.contains("content://")){

                val cr = this.contentResolver
                list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))

            }else{
                list.add(selectList[0].path)
            }
           // vm.toUpImg(list[0])

            OssUtils(this, vm)
                .setUpLoadMore("img", list, vm.type, this)

            return
        }

    }

    override fun onUploadSuccess(t: UpImgBean) {

        var index = t.position

        when (index) {
            1 -> {
                vm.imgOne.value= t.url
            }
            2 -> {
                vm.imgTwo.value= t.url
            }

            3 -> {
                vm.imgThree.value= t.url

            }
            4 -> {
                vm.imgFour.value= t.url
            }


        }
    }

    override fun onUploadError(error: String?) {
    }


}
